<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <title>登录</title>
	<link rel="stylesheet" href="#(ctx_path)/lib/bootstrap/css/bootstrap.css">
	<style type="text/css">
		body {width:100%;height:100%;background:url('#(ctx_path)/lib/bootstrap/img/background.jpg') no-repeat; background-size:100% 100%; }
	</style>
</head>
<body>
<div style="padding: 60px 80px 0px 80px;">
	<div class="text-center"  style="margin-bottom:60px;">
		<img src="#(ctx_path)/lib/bootstrap/img/dj_logo.png"  alt=""  style="width:120px;height:120px;" />
		<h3 style="margin-top:0px; "></h3>
	</div>
	<form class="bs-example bs-example-form" role="form" action="" method="post" id="loginform">
		<div class="input-group">
			<span class="input-group-addon"><span class="glyphicon glyphicon-phone"></span></span>
			<input type="text" class="form-control required" id="phone" placeholder="请输入手机号..." maxlength="11">
		</div>
		<br>
		<div class="input-group">
			<input type="text" class="form-control required" id="code" placeholder="请输入验证码...">
			<input type="hidden" id="random" value="">
			<div class="input-group-addon" id="sms" style="cursor:pointer;" onclick="getCode()">获取验证码</div>
		</div>
		<br>
		<div class="agree"  style="font-size:10px;color: #0a6999;">
			<span><a href="#(ctx_path)/mobile/productList/showUserAgreement.html">注册即表示同意《用户注册协议》</a></span>
		</div>
		<div style="padding-top:3px;">
		    <button type="button" class="btn btn-success btn-block pull-right" onclick="login();">登&nbsp;录&nbsp;免&nbsp;注&nbsp;册</button>
		</div>
	</form>
</div>   
</body>
<script src="#(ctx_path)/lib/bootstrap/js/jquery-1.10.2.min.js"></script>
<script src="#(ctx_path)/lib/bootstrap/js/bootstrap.js"></script>
<script type="text/javascript" src="#(ctx_path)/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="#(ctx_path)/lib/JSUtils/Tools.js"></script>
<script type="text/javascript" src="#(ctx_path)/lib/JSUtils/AndriodIso.js"></script>
<script type="text/javascript">

   var type = "";
    window.onload=function(){
        changeHeight();
        setTimeout(function(){
            setupWebViewJavascriptBridge(function(bridge) {
                bridge.callHandler(
                    'paraToH5'
                    , ""
                    , function (responseData) {
                        console.log("JS received response:", responseData);
                        type = responseData;
                    }
                );
            });
		},1000);
    }
   var toClick = true;
   function smsclick($obj, time) {
       if (time == 0) {
           $obj.attr("disabled", false);
           $obj.css("cursor", "pointer");
           $obj.text("获取验证码");
           toClick = true;
           return;
       } else {
           $obj.attr("disabled", true);
           $obj.css("cursor", "not-allowed");
           $obj.text("重新发送(" + time + ")");
           time--;
           toClick = false;
       }
       setTimeout(function () { smsclick($obj, time) }, 1000)
   }
    function changeHeight(){
        $("body").height($(window).height());
        //document.getElementsByTagName('body')[0].style.height =window.screen.height+'px';
    }
    var telHid = 0;
    var randomCode;
    var sent = false;		//防止验证码重复发送
    function getCode() {
        var phone = $("#phone").val();
		if (!checkPhone(phone)){
            layer.msg('手机号码不正确！', {icon: 5,time:1500});
			return;
		}
		if (!toClick){
		    return;
		}
		if (sent){
            layer.msg('验证码已发送，请耐心！', {icon: 2,time:1500});
            return;
		}
		sent = true;
        smsclick($("#sms"),60);
        var source = top.location.href.split("?")[1];
        if (source == undefined || !isExist("source",source.split("="))){
            source = "undefined";
        }else{
            source = source.split("&")[0];
        }
        $.ajax({
            type: 'POST',
            url: '#(ctx_path)/mobile/productList/sendSmsCode.html',
            data:{"telephone":phone,"source":source},
            dataType: 'json',
            success: function(data){
                if (data.flag == "true"){
                    telHid = data.telHid;
                    randomCode = data.random;
                    $("#random").val(randomCode);
                    layer.msg(data.result, {icon: 1,time:1500});
                }else {
                    layer.msg(data.result, {icon: 5,time:1500});
				}
				sent = false;
            },
            error:function(data) {
                layer.msg('系统错误，请联系管理员！', {icon: 5,time:1500});
                console.log(data.msg);
            }
        });
    }

    function login(){
        var phone = $("#phone").val().trim();
        if (!checkPhone(phone)){
            layer.msg('手机号码不正确！', {icon: 5,time:1500});
            return;
        }
        var code = $("#code").val();
        if (code == ''){
            layer.msg('请输入手机验证码！', {icon: 5,time:1500});
            return;
        }
        if (code != randomCode){
            layer.msg('验证码不正确！', {icon: 5,time:1500});
            return;
        }
        $.ajax({
            type: 'POST',
            url: '#(ctx_path)/mobile/productList/telLogin.html',
            data:{"tclh.id":telHid,"tclh.telephone":phone},
            dataType: 'json',
            success: function(data){
                //if login successfully, send sessionId to app and forward home page
                if (data.isSuccess == true){
                    //客户端已经注册好一个名为“webCall”的方法，H5直接进行调用（方法名也为“webCall”）就行，调用的时候可以传客户端需要的参数
                    if (isWeiXin()){
                        location.href = "#(ctx_path)/mobile/productList";
                        return;
					}

					if(type != null && type != "null" && type != ""){
                        try{
                            setupWebViewJavascriptBridge(function(bridge) {
                                bridge.callHandler(
                                    'successLogin'
                                    , data.result
                                    , function (responseData) {
                                        console.log("JS received response:", responseData)
                                    }
                                );
                            });
						}catch (e) {
                            location.href = "#(ctx_path)/mobile/productList";
                        }
                    }else{
                        location.href = "#(ctx_path)/mobile/productList";
                    }

                }else {
                    layer.msg(data.result, {icon: 2,time:1500});
                }
            },
            error:function(data) {
                layer.msg('系统错误，请联系管理员！', {icon: 5,time:1500});
                console.log(data.msg);
            }
        });
    }

</script>
</html>